package com.smile.weather.presenter;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.smile.weather.bean.LifeIndexData;
import com.smile.weather.bean.TodayWeatherData;
import com.smile.weather.bean.TwoDayWeatherData;
import com.smile.weather.interfaces.ITodayFragment;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Created by smile on 2016/8/22.
 */
public class TodayPresenter {
    private Context mContext;
    private ITodayFragment iTodayFragment;
    private TwoDayWeatherData[] twoDayWeather;
    private LifeIndexData lifeIndex;

    public TodayPresenter(Context mContext) {
        this.mContext = mContext;
    }

    public void setiTodayFragment(ITodayFragment iTodayFragment) {
        this.iTodayFragment = iTodayFragment;
    }

    public void analyzeHtml(String html) {
        Document content = Jsoup.parse(html);

        dealTwo(content);
        dealLife(content);
    }

    private void dealTwo(Document content) {
        twoDayWeather = new TwoDayWeatherData[2];
        Elements element = content.getElementsByAttributeValue("class", "clearfix");
        Elements element2 = content.getElementsByClass("jpg80");
        Elements element3 = content.getElementsByClass("sun");

        Elements e = element.get(0).getElementsByTag("li");

        for (int n = 0; n < 2; ++n) {
            Elements e2 = e.get(n).children();
            twoDayWeather[n] = new TwoDayWeatherData();
            twoDayWeather[n].setDate(e2.get(0).text());
            twoDayWeather[n].setTime(e2.get(5).text());
            twoDayWeather[n].setTemperature(e2.get(3).text().replace("°C", ""));
            twoDayWeather[n].setWeather(e2.get(2).text());
            twoDayWeather[n].setWind(e2.get(4).text());
            twoDayWeather[n].setWeather_icon(element2.get(n).className().replace("jpg80 ", "").toLowerCase());
            twoDayWeather[n].setTime_icon(element3.get(n).className().replace("sun ", "").toLowerCase());

            Log.i("tp", twoDayWeather[n].toString());
        }
        iTodayFragment.setTwoDayWeather(twoDayWeather);

    }

    private void dealLife(Document content) {
        Elements element = content.getElementsByAttributeValue("class", "clearfix");
        Elements span = element.get(2).getElementsByTag("span");
        Elements p = element.get(2).getElementsByTag("p");

        String[][] values = new String[6][2];

        for(int i = 0;i < 6;++i){
            values[i][0] = span.get(i).text();
        }
        for(int i = 0;i < 6;++i){
            values[i][1] = p.get(i).text();
        }
        lifeIndex = new LifeIndexData();
        lifeIndex.setUv_index(values[0]);
        lifeIndex.setCode_index(values[1]);
        lifeIndex.setDressing_index(values[2]);
        lifeIndex.setCar_index(values[3]);
        lifeIndex.setSports_index(values[4]);
        lifeIndex.setAir_pollution_index(values[5]);

        iTodayFragment.setLifeIndex(lifeIndex);
    }

}
